package iscte.ai.kcgtd;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper{

	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "personalSettings";

	// Contacts table name
	private static final String TABLE_SETTINGS = "settings";

	// Contacts Table Columns names
	private static final String KEY_ID = "id";
	private static final String KEY_FIRSTNAME = "first_name";
	private static final String KEY_LASTNAME = "last_name";
	private static final String KEY_EMAIL = "email";
	private static final String KEY_STARTHOUR = "start_hour";
	private static final String KEY_STARTMINUTES = "start_minutes";
	private static final String KEY_ENDHOUR = "end_hour";
	private static final String KEY_ENDMINUTES = "end_minutes";

	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_SETTINGS + "("
				+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FIRSTNAME + " TEXT,"
				+ KEY_LASTNAME + " TEXT," + KEY_EMAIL + " TEXT," + KEY_STARTHOUR + " INTEGER," 
				+ KEY_STARTMINUTES + " INTEGER," + KEY_ENDHOUR + " INTEGER," 
				+ KEY_ENDMINUTES + " INTEGER" +")";
		db.execSQL(CREATE_CONTACTS_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_SETTINGS);
		// Create tables again
		onCreate(db);
	}

	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */

	// Adding new settings
	void addSettings (Settings settings) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_FIRSTNAME, settings.getFirstName());
		values.put(KEY_LASTNAME, settings.getLastName());
		values.put(KEY_EMAIL, settings.getEmail());
		values.put(KEY_STARTHOUR, settings.getStartHour());
		values.put(KEY_STARTMINUTES, settings.getStartMinutes());
		values.put(KEY_ENDHOUR, settings.getEndHour());
		values.put(KEY_ENDMINUTES, settings.getEndMinutes());

		// Inserting Row
		db.insert(TABLE_SETTINGS, null, values);
		db.close(); // Closing database connection
	}

	// Getting single setting
	Settings getSettings() {
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.query(TABLE_SETTINGS, new String[] { KEY_ID,
				KEY_FIRSTNAME, KEY_LASTNAME, KEY_EMAIL, KEY_STARTHOUR,
				KEY_STARTMINUTES, KEY_ENDHOUR, KEY_ENDMINUTES}, KEY_ID + "=?",
				new String[] { String.valueOf(1) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();

		Settings settings = new Settings(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), cursor.getString(2), cursor.getString(3),
				Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)),
				Integer.parseInt(cursor.getString(6)), Integer.parseInt(cursor.getString(7)));
		
		return settings;
	}

	// Updating single settings
	public int updateSettings(Settings settings) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_FIRSTNAME, settings.getFirstName());
		values.put(KEY_LASTNAME, settings.getLastName());
		values.put(KEY_EMAIL, settings.getEmail());
		values.put(KEY_STARTHOUR, settings.getStartHour());
		values.put(KEY_STARTMINUTES, settings.getStartMinutes());
		values.put(KEY_ENDHOUR, settings.getEndHour());
		values.put(KEY_ENDMINUTES, settings.getEndMinutes());

		// updating row
		return db.update(TABLE_SETTINGS, values, KEY_ID + " = ?",
				new String[] { String.valueOf(settings.getID()) });
	}

	// Deleting single settings
	public void deleteSettings(Settings settings) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_SETTINGS, KEY_ID + " = ?",
				new String[] { String.valueOf(settings.getID()) });
		db.close();
	}
	
	// Getting All Settings
		public List<Settings> getAllSettings() {
			List<Settings> settingsList = new ArrayList<Settings>();
			// Select All Query
			String selectQuery = "SELECT  * FROM " + TABLE_SETTINGS;

			SQLiteDatabase db = this.getWritableDatabase();
			Cursor cursor = db.rawQuery(selectQuery, null);

			// looping through all rows and adding to list
			if (cursor.moveToFirst()) {
				do {
					Settings settings = new Settings();
					settings.setID(Integer.parseInt(cursor.getString(0)));
					settings.setFirstName(cursor.getString(1));
					settings.setLastName(cursor.getString(2));
					settings.setEmail(cursor.getString(3));
					settings.setStartHour(Integer.valueOf(cursor.getString(4)));
					settings.setStartMinutes(Integer.valueOf(cursor.getString(5)));
					settings.setEndHour(Integer.valueOf(cursor.getString(6)));
					settings.setEndMinutes(Integer.valueOf(cursor.getString(7)));
					// Adding settings to list
					settingsList.add(settings);
				} while (cursor.moveToNext());
			}

			return settingsList;
		}

}